package org.ricks.virtual.vo;


import dev.morphia.annotations.*;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

@Entity("p_role")
@Indexes(
        {
                @Index(options = @IndexOptions(name = "idx_role_id"), fields = @Field("id")),
                @Index(options = @IndexOptions(name = "idx_name_id"), fields = @Field("name"))
        }
)
@Getter
@Setter
public class RoleVo {
    @Id
    private String id;

    private SdkInfo sdkInfo = new SdkInfo();
    /**
     * 名称
     */
    private String name;

    /**
     * 名称修改次数
     */
    private int renameNum;

    /**
     * 穿戴的头像ID
     */
    private int headId;

    /**
     * 区服
     */
    private int sid;

    private int lv = 1;
    private int exp;

    /**
     * 体力
     */
    private int energy;
    /**
     * 上一次恢复体力时间
     */
    private long recoverEnergyTime;
    /**
     * 普通体力已购买次数
     */
    private int normalEnergyBuyTimes;
    /**
     * 高级体力已购买次数
     */
    private int highEnergyBuyTimes;
    /**
     * 上次购买体力次数重置时间
     */
    private long lastBuyTimesResetTime;
    /**
     * 可领体力次数
     */
    private int energyReceiveTimes;
    /**
     * 上次体力次数增加时间
     */
    private long lastEnergyAddTime;


    /**
     * 兵书 key:type
     */
    private Map<Integer, Set<Integer>> science = new HashMap<>();

    /**
     * 上次登录时间
     */
    private long loginTime;
    /**
     * 上次离线时间
     */
    private long offlineTime;
    /**
     * 创角时间
     */
    private long createTime;
    /**
     * 上一次每日重置时间
     */
    private long resetTime;
    /**
     * 已完成的新手引导id
     */
    private Set<Integer> completeGuides = new HashSet<>();

    /**
     * 封号到期时间 -1:永久封号  0:不封号
     */
    private long banTime;

    private Map<Integer, BaseAttribute> attributeMap = new HashMap<>();

    /**
     * 客户端自定义数据
     */
    private Map<String, byte[]> customData = new HashMap<>();

    /**
     * 解锁头像
     */
    private Set<Integer> unlockHead = new HashSet<>();

    /**
     * 战斗力
     */
    private long combatNum;

    /**
     * 免广告时间
     */
    private long freeADTime;

    /**
     * 加入得工会ID
     */
    private String guildId = "";

    /**
     * 上次工会退出时间
     */
    private long lastLogoutGuild;



    public int getNormalEnergyBuyTimes() {
        if (checkReset()) {
            normalEnergyBuyTimes = 0;
            highEnergyBuyTimes = 0;
        }
        return normalEnergyBuyTimes;
    }

    public int getHighEnergyBuyTimes() {
        if (checkReset()) {
            normalEnergyBuyTimes = 0;
            highEnergyBuyTimes = 0;
        }
        return highEnergyBuyTimes;
    }

    private boolean checkReset() {
        Calendar calendar = Calendar.getInstance();
        Calendar lastCalendar = Calendar.getInstance();
        lastCalendar.setTimeInMillis(lastBuyTimesResetTime);
        if (lastCalendar.get(Calendar.DAY_OF_YEAR) != calendar.get(Calendar.DAY_OF_YEAR)) {
            lastBuyTimesResetTime = calendar.getTimeInMillis();
            return true;
        }
        return false;
    }
}
